<?php


class Controller extends Base
{
    private $name, $action, $newView, $data, $partialViews=array();
    public $view='index', $params, $route, $theme;

    public function __construct()
    {
        $this->name=get_class($this);
    }

    public function run()
    {
        if(!$this->setView()){
            Sys::app()->error='Невозможно загрузить представление '.$this->view.' в котроллере '.$this->name;
            Request::redirect(404);
        }
        $action=$this->action;
        Sys::app()->controller->$action();
    }

    public function render($view='',$data=array())
    {
        if(!$data && is_array($view)){
            $data=$view;
            $view=$this->action;
        }
        $error='Невозможно загрузить представление '.$view.' в котроллере '.$this->name;
        if(!$this->setView($view)){
            Sys::app()->error=$error;
            Request::redirect(404);
        }
        if(!Sys::app()->request->isAjax){
            $this->data=$data;
            $theme=$this->themeUrl;
            $scripts=Sys::app()->scripts;
            require_once($this->loadTheme());
        }else{
            $json=array(
                'content'=>'',
                'error'=>'',
            );

            if($viewHtml=$this->renderView($view,$data)) $json['content']=$viewHtml;
            else{
                Sys::app()->error=$error;
                $json['error']=Sys::app()->error;
            }

            exit(json_encode($json));
        }
    }

    public function renderPartial($view='',$data=array())
    {
        if(is_array($view) && !$data){
            $data=end($view);
            $view=array_shift($view);
        }

        if(strpos($view,SEP)!==false || strpos($view,'/')!==false) $file=$this->searchFile($view);
        else{
            if(!$view) $view=$this->action;
            $route=HTML.lcfirst($this->name).SEP.$this->action.SEP.'partials';
            if(!$file=$this->searchFile($view,$route)) $file=$this->searchFile($view,HTML);
        }
        if($file){
            foreach($data as $name=>$value){
                $$name=$value;
            }
            require_once($file);
            return '';
        }else{
            Sys::app()->error='Попытка загрузить несущестувующее частичное представление: '.$view;
            return false;
        }
    }

    public function redirect($route,$params=array())
    {
        Request::redirect($route,$params);
    }

    public function setModel($value)
    {
        $this->model=Model::model($value);
    }

    public function setView($view='')
    {
        if(!$view) $view=$this->action;
        if(strpos($view,BASE_DIR)!==false){
            $patch=$view;
            $view='';
        }else $patch=HTML.lcfirst(str_replace('Controller','',$this->name));
        if($file=$this->searchFile($view,$patch)) return $this->view=$file;
        elseif($file=$this->searchFile('index',$patch)) return $this->view=$file;
        elseif($file=$this->searchFile($view,HTML)) return $this->view=$file;
        elseif($file=$this->searchFile('index',HTML)) return $this->view=$file;
        else return false;
    }

    public function setAction($value)
    {
        if(method_exists($this,$value)) $this->action=$value;
    }

    public function getAction()
    {
        return $this->action;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getContent()
    {
        if($this->view){
            if($this->data){
                foreach($this->data as $name=>$value){
                    $$name=$value;
                }
            }
            require_once($this->view);
            $scripts=Sys::app()->scripts;
            foreach($scripts as $script){
                echo '<script type="text/javascript" src="'.$script.'"></script>';
            }
        }else
            Request::redirect(404);

        return '';
    }

    public function getThemeUrl()
    {
        return BASEURL.'app'.SEP.'themes'.SEP.$this->theme.SEP;
    }

    protected function loadTheme()
    {
        return THEMES.$this->theme.SEP.'index.php';
    }
}